package com.breeze.stack;

/**
 * 顺序栈
 * 支持栈创建、出栈、入栈、栈大小，栈清空
 */
public class SequenceStack<T> {
    private T[] datas;
    private int top = -1;
    private final int MAX_SIZE = 10;

    public SequenceStack() {
        datas = (T[]) new Object[MAX_SIZE];
    }

    public SequenceStack(int maxSize) {
        if (maxSize <= 0) {
            throw new RuntimeException("数组长度要大于0");
        }
        this.datas = (T[]) new Object[maxSize];
    }

    public T pop() {
        if (isEmpty()) {
            System.out.println("栈为空");
            return null;
        }
        return datas[top--];
    }

    public void push(T data) {
        if (top == datas.length) {
            System.out.println("栈已满");
            return;
        }
        datas[++top] = data;
    }

    public int size() {
        return top + 1;
    }

    public void clear() {
        top = -1;
    }

    public boolean isEmpty() {
        return top == -1;
    }
}
